package com.emagic.manage.common.ui.base;

import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.StringRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.emagic.manage.R;


/**
 * 带ToolBar及TabLayout的Activity基类
 * 基于Android Design Support Library , see {@link CoordinatorLayout}
 *
 * @author baoy
 * @version 1.0
 *          Create by 15/12/14 下午4:39
 */
public class TabActivity extends AppStateActivity {

    protected Toolbar mToolbar;
    protected TabLayout mTabLayout;

    private TextView mToolbarTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar();
    }

    private void initToolBar() {
        /*通过inflater获取toolbar的布局文件*/
        View view = getLayoutInflater().inflate(R.layout.appbar_layout_with_tab, getRootView());
        mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar);
        mToolbarTitle = (TextView) view.findViewById(R.id.tool_bar_title);
        mTabLayout = (TabLayout) view.findViewById(R.id.id_tab_layout);

        mToolbar.setTitle("");
        //设置toolbar到Activity
        setSupportActionBar(mToolbar);
    }


    public Toolbar getToolbar() {
        return mToolbar;
    }

    public TabLayout getTabLayout() {
        return mTabLayout;
    }

    public void setToolbarTitle(String title) {
        this.mToolbarTitle.setText(title);
    }

    public void setToolbarTitle(@StringRes int res) {
        this.mToolbarTitle.setText(res);
    }

    public void setToolbarTitleColorRes(@ColorRes int id) {
        int color = getResources().getColor(id);
        mToolbarTitle.setTextColor(color);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
